home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ddjgrep.zip / BITMAP.C next >
Text File  |  1987-11-04  |  2KB  |  118 lines

  1. /*    BITMAP.C    makebitmap, setbit, testbit: bit map manipulation
  2.  *            routines.
  3.  *
  4.  *    Copyright (c) 1985, Allen I. Holub, all rights reserved.
  5.  *    This program may be copied for personal, non-profit use only.
  6.  */
  7.  
  8. extern char    *calloc        ( unsigned, unsigned );
  9.  
  10.  
  11.  
  12. #ifdef DEBUG
  13. #include <stdio.h>
  14. #endif
  15.  
  16. typedef char BITMAP;
  17.  
  18. /*----------------------------------------------------------------------*/
  19.  
  20. BITMAP    *makebitmap( size )
  21. unsigned  size;
  22. {
  23.     /*    Make a bit map with "size" bits. The first entry in
  24.      *    the map is an  unsigned int representing the maximum
  25.      *    bit. The map itself is concatenated to this integer.
  26.      *    Return a pointer to the map on success, 0 if there's
  27.      *    not enough memory.
  28.      */
  29.     
  30.     unsigned *map, numbytes;
  31.     
  32.     numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 ) ;
  33.  
  34. #ifdef DEBUG
  35.     printf("Making a %d bit map (%d bytes required)\n", size, numbytes);
  36. #endif
  37.  
  38.     if( map = (unsigned *) calloc( numbytes + sizeof(unsigned) ,1 ) )
  39.         *map = size;
  40.  
  41.     return (BITMAP *) map;
  42. }
  43.  
  44. setbit( c, map, val )
  45. unsigned  c, val;
  46. char      *map;
  47. {
  48.     /*   Set bit c in the map to val.
  49.      *   If c > map size, 0 is returned, else 1 is returned.
  50.      */
  51.  
  52.     if(  c >= *(unsigned *)map )        /* if c >= map size */
  53.         return 0;
  54.  
  55.     map += sizeof(unsigned);        /* Skip past size   */
  56.  
  57.     if( val )
  58.         map[c >> 3]  |=    1 << (c & 0x07) ;
  59.     else
  60.         map[c >> 3]  &=  ~(1 << (c & 0x07)) ;
  61.  
  62.     return( 1 );
  63. }
  64.  
  65.  
  66. /* ------------------------------------------------------------------- */
  67.  
  68.  
  69. testbit( c, map )
  70. unsigned c;
  71. char     *map;
  72. {
  73.     /*   Return 1 if the bit corresponding to c in map is set.
  74.      *   0 if it is not.
  75.      */
  76.  
  77.     if( c >= *(unsigned *)map )
  78.         return 0;
  79.  
  80.     map += sizeof(unsigned);
  81.  
  82.     return(  map[ c >> 3 ]  &  (1 << (c & 0x07)) );
  83. }
  84.  
  85. #ifdef DEBUG
  86.  
  87. main()
  88. {
  89.     int    bitnum, set, i, *map;
  90.  
  91.     printf("Making a 32 bit wide bit map\n");
  92.  
  93.     if( !(map = makebitmap( 32 )) )
  94.         printf("Can't make map\n");
  95.  
  96.     while( 1 )
  97.     {
  98.         /* Print the bit map. Try to print past the end of the
  99.          * map to make sure overflow detection works (bit 32 should
  100.          * come back as a 0).
  101.          */
  102.  
  103.         for( i = 0; i <= 32 ; i++ )
  104.             putchar( testbit( i, map ) ? 'X' : '.' );
  105.  
  106.         printf("\n\nBit number :");
  107.         scanf("%d", &bitnum );
  108.         printf("\n1 to set, 0 to clear: ");
  109.         scanf("%d", &set );
  110.  
  111.         if( ! setbit(bitnum, map, set) )
  112.             printf("Bit out of range\n");
  113.     }
  114. }
  115.  
  116. #endif
  117. 
  118.